package com.sanpu.kxe.business.login.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.PermissionActivity;
import com.sanpu.kxe.business.login.bean.LoginOutput;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.view.MainActivity;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.utils.AppContextUtil;
import com.sanpu.kxe.utils.IntentTool;
import com.sanpu.kxe.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Description：APP闪屏页面
 * Created by 薛勇军 on 2018/6/12.
 */

public class SplashActivity extends PermissionActivity {

    private int loadTime = 2000;

    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTaskRoot()) {
            finish();
            return;
        }
        Window window = getWindow();
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);

        setContentView(R.layout.activity_splash);
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.fl_splash_container);
        frameLayout.setBackgroundResource(R.drawable.oem_img_splash);

        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                enterMain();
            }
        }, loadTime);
    }

    private synchronized void enterMain() {
        boolean welcome = CacheManage.getInstance().getCache(CacheModel.WELCOME, true);
        if (welcome) {
            IntentTool.startActivity(this, WelcomeNewActivity.class, true);
            return;
        }
        try {
            LoginOutput loginOutput = CacheManage.getInstance().getCache(CacheModel.LOGIN_KEY);
            UserCenterOutput userCenterOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
            if (loginOutput != null && userCenterOutput != null) {
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra(CommonConfig.INTENT_ISTRANSPUSH,
                        getIntent().getBooleanExtra(CommonConfig.INTENT_ISTRANSPUSH, false));
                IntentTool.startActivity(this, intent, true);
            } else {
                if (userCenterOutput != null) {
                    ToastUtil.showTextViewPromptLong("登陆过期，请重新登录");
                }
                IntentTool.startActivity(this, LoginActivity.class, true);
            }
        } catch (ClassCastException e) {
            CacheManage.getInstance().clear();
            AppContextUtil.finishAllExclByActivity(SplashActivity.class.getName());
            IntentTool.startActivity(this, LoginActivity.class, true);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getName()); // 统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this); // 统计时长
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}
